package cn.com.dhc.danlu.business.shopcar.delegate;

import java.util.List;

import cn.com.dhc.danlu.activity.delivery.order.utill.VisitableWrapper;
import cn.com.dhc.danlu.business.shopcar.bean.ShopCarItem;
import cn.com.dhc.danlu.business.shopcar.util.ModelFlatUtils;
import cn.com.dhc.danlu.delegate.callback.IListDataCallback;
import cn.com.dhc.danlu.delegate.delegate.list.BaseListDataDelegate;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.hd.mall.web.webservices.entity.BaseResponse;
import cn.com.hd.mall.web.webservices.entity.freedom.request.base.BaseRequest;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse;
import rx.Observable;
import rx.functions.Func1;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-09 10:59
 *
 * <p>
 * 购物车列表信息的委托
 * </p>
 */
public class ShopCarListDelegate extends BaseListDataDelegate<IListDataCallback> {
    @Override
    protected Observable createRequestObservable(int page) {
        BaseRequest request = new BaseRequest();
        request.setToken(agent.getToken());

        Observable<VisitableWrapper<ShopCarItem>> observable =
                agent.getApi().toShoppingCart(request)
                        .compose(OperatorUtils.<ToShoppingcartResponse> proccessError())
                        .map(new Func1<ToShoppingcartResponse, VisitableWrapper<ShopCarItem>>() {
                            @Override
                            public VisitableWrapper<ShopCarItem> call(ToShoppingcartResponse res) {
                                VisitableWrapper<ShopCarItem> wrapper = new VisitableWrapper<>();
                                wrapper.setSuccess(res.getSuccess());
                                wrapper.setDatas(ModelFlatUtils.flatShopCarResponse(res));
                                return wrapper;
                            }
                        });

        return OperatorUtils.convertThread(observable);
    }

    @Override
    protected List excavateList(BaseResponse model) {
        VisitableWrapper wrapper = (VisitableWrapper) model;
        return wrapper.getDatas();
    }
}
